package com.ideaaedi.plugin.fastcoding.ui;

import com.alibaba.fastjson.JSON;
import com.ideaaedi.plugin.fastcoding.constant.FastCodingConstant;
import com.ideaaedi.plugin.fastcoding.util.JdPsiClassUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.PsiType;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.util.ui.JBUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Comparator;

/**
 * 请选择
 *
 * @author JustryDeng
 * @since 2022/1/6 23:45:49
 */
public class MockJson_ChooseAndShowJson extends JFrame {
    private JPanel jPanel;
    
    public MockJson_ChooseAndShowJson(Project project, java.util.List<PsiType> typeList) {
        // 初始化jPanel 并动态添加item
        initAndDynamicAddItem(project, typeList);
        
        // 设置内容面板
        setContentPane(jPanel);
        // 设置标题
        setTitle(FastCodingConstant.PLUGIN_NAME + FastCodingConstant.CONCAT_SIGN_WITH_WHITESPACE + FastCodingConstant.MOCK_JSON_NAME);
    
        // 设置大小 和 位置
        String maxLengthPresentableText = typeList.stream()
                .map(PsiType::getPresentableText)
                .max(Comparator.comparing(String::length))
                .orElseThrow(() -> new RuntimeException("Cannot get maxLengthPresentableText"));
        int maxWidth = maxLengthPresentableText.length() * 8;
        setSize(Math.max(400, maxWidth), 50 * (typeList.size() + 1));
        setLocationRelativeTo(null);
    }
    
    /**
     * 初始化jPanel 并动态添加 item
     */
    private void initAndDynamicAddItem(Project project, java.util.List<PsiType> typeList) {
        jPanel = new JPanel();
        jPanel.setLayout(new GridLayoutManager(typeList.size() + 1, 1, JBUI.emptyInsets(), -1, -1));
        JLabel pleaseChooseTips = new JLabel();
        pleaseChooseTips.setText("识别到多个类，请选择其中一个进行mock");
        pleaseChooseTips.setIcon(Messages.getInformationIcon());
        jPanel.add(pleaseChooseTips, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST,
                GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW,
                GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
    
        java.util.List<JButton>  jButtonList = new ArrayList<>(8);
        for (int i = 0; i < typeList.size(); i++) {
            JButton button = new JButton();
            jButtonList.add(button);
            final int currIndex = i;
            final PsiType psiType = typeList.get(i);
            button.setText(psiType.getPresentableText());
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    MockJson_ShowJson.invokeShowJson(project, psiType);
                }
            });
    
            // 上下键按钮与回车
            button.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent keyEvent) {
                    super.keyPressed(keyEvent);
                    int keyCode = keyEvent.getKeyCode();
                    if (keyCode == KeyEvent.VK_ENTER) {
                        MockJson_ShowJson.invokeShowJson(project, psiType);
                    } else if (keyCode == KeyEvent.VK_UP) {
                        if (currIndex == 0) {
                            jButtonList.get(jButtonList.size() - 1).grabFocus();
                        } else {
                            jButtonList.get(currIndex - 1).grabFocus();
                        }
                    } else if (keyCode == KeyEvent.VK_DOWN) {
                        if (currIndex == jButtonList.size() - 1) {
                            jButtonList.get(0).grabFocus();
                        } else {
                            jButtonList.get(currIndex + 1).grabFocus();
                        }
                    }
                }
            });
            
            jPanel.add(button, new GridConstraints(i + 1, 0, 1, 1, GridConstraints.ANCHOR_CENTER,
                    GridConstraints.FILL_HORIZONTAL,
                    GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                    GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        }
    }
    

    
}
